package com.fan.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fan.reggie.common.CustomException;
import com.fan.reggie.entity.Category;
import com.fan.reggie.entity.Dish;
import com.fan.reggie.entity.Setmeal;
import com.fan.reggie.mapper.CategroyMapper;
import com.fan.reggie.service.CategoryService;
import com.fan.reggie.service.DishService;
import com.fan.reggie.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategroyMapper, Category> implements CategoryService {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private DishService dishService;

    /*
     * 根据id删除分类，删除之前需要判断是否关联的菜品
     * */
    @Override
    public void remove(Long id){
        //添加查询条件，根据分类id进行查询
        LambdaQueryWrapper<Dish> dishqueryWrapper = new LambdaQueryWrapper();
        dishqueryWrapper.eq(Dish::getCategoryId, id);
        int count = dishService.count(dishqueryWrapper);
        //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
        if(count>0){
            //抛出一个异常
            throw new CustomException("当前分类项关联了菜品，不能删除");
        }
        //查询是否关联了套餐
        LambdaQueryWrapper<Setmeal> setMealQueryWrapper = new LambdaQueryWrapper();
        setMealQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count1 = setMealService.count(setMealQueryWrapper);
        if(count1>0){
            //抛出一个异常
            throw new CustomException("当前分类项关联了套餐，不能删除");
        }
        //正常删除分类
        super.removeById(id);

    }
}
